package com.tcfvic.server.upload;

import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Cell;
import jxl.DateCell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.ProgressListener;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.log4j.Logger;
import org.datanucleus.util.StringUtils;

import com.tcfvic.client.PersonService;
import com.tcfvic.client.pojos.Address;
import com.tcfvic.client.pojos.Course;
import com.tcfvic.client.pojos.Employmentdetails;
import com.tcfvic.client.pojos.EmploymentdetailsId;
import com.tcfvic.client.pojos.Expense;
import com.tcfvic.client.pojos.ExpenseId;
import com.tcfvic.client.pojos.Fundingsource;
import com.tcfvic.client.pojos.Person;
import com.tcfvic.client.pojos.Personaddress;
import com.tcfvic.client.pojos.Personcourse;
import com.tcfvic.client.pojos.PersoncourseId;
import com.tcfvic.client.pojos.Personcoursefundingsource;
import com.tcfvic.client.pojos.PersoncoursefundingsourceId;
import com.tcfvic.client.pojos.Personfundingsource;
import com.tcfvic.client.pojos.PersonfundingsourceId;
import com.tcfvic.client.pojos.Rto;
import com.tcfvic.client.pojos.Workercompany;
import com.tcfvic.server.PersonServiceImpl;

public class ExcelTCVVicFormat extends HttpServlet implements IExcelFormat {
	private static final long serialVersionUID = 5567237221724423872L;

	private static final Logger logger = Logger
			.getLogger(ExcelTCVVicFormat.class);

	private List<String> errorList = new ArrayList<String>();
	private int numberImported = 0;

	private final SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
	private Map<Integer, Person> persons;
	private Map<String, Course> courses;
	private Map<String, Rto> rtos;
	private Map<String, Fundingsource> fundingSources;
	private Map<String, Workercompany> companies;
	private PersonService personService = new PersonServiceImpl();

	private final String PAC_BRANDS_NAME = "Pacific Brands";
	private final String PAC_BRANDS_DELIVERED_VIA = "Pacific Brands";
	private final int PAC_BRANDS_INITIAL_AMOUNT = 3000;
	private final int PAC_BRANDS_INITIAL_DAYS = 15;
	private final int PAC_BRANDS_INITIAL_HOURS = 114;
	private final String SKILL_UP_NAME = "Skill up";
	private final String SKILL_UP_DELIVERED_VIA = "Vic Government";
	private final int SKILL_UP_INITIAL = 800;
	private final String TCFSAP_NAME = "TCF-SAP";
	private final String TCFSAP_DELIVERED_VIA = "CTH Government";
	private final int TCFSAP_INITIAL = 1350;
	private final String SAP_PPP_NAME = "SAP-PPP";
	private final String SAP_PPP_DELIVERED_VIA = "CTH Government";
	private final int SAP_PPP_INITIAL = 1;

	public ExcelTCVVicFormat() {
		logger.info("Uploading a Kirstee file format.");
		personService = new PersonServiceImpl();
		initialise();
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/plain");
		logger.info("Inside TCF Kirstee File Upload Kirstee Servlet...............>>>>>>>>>>");
		personService = new PersonServiceImpl();
		initialise();
		try {
			getFileItem(request);
		} catch (Exception e) {
			logger.error(e);
		}
	}

	@SuppressWarnings("unchecked")
	private FileItem getFileItem(final HttpServletRequest request) {
		FileItemFactory factory = new DiskFileItemFactory();
		ServletFileUpload upload = new ServletFileUpload(factory);
		// Create a progress listener, if want to use
		ProgressListener progressListener = new ProgressListener() {
			private long megaBytes = -1;
			int i = -1;

			public void update(long pBytesRead, long pContentLength, int pItems) {
				if (i != pItems) {
					i = pItems;
				}
				long mBytes = pBytesRead / 10000;
				if (megaBytes == mBytes) {
					return;
				}
				megaBytes = mBytes;
			}
		};
		upload.setProgressListener(progressListener);

		try {
			List items = upload.parseRequest(request);
			Iterator it = items.iterator();
			while (it.hasNext()) {
				FileItem item = (FileItem) it.next();
				if (!item.isFormField()) {
					this.processUpload(item);
				}
			}
		} catch (Exception e) {
			logger.error(e);
			return null;
		}
		return null;
	}

	private void processUpload(FileItem uploadItem) {
		try {
			String uploadDir = getServletContext().getRealPath("/") + "tcfuaUplaod/";
			File file = new File(uploadDir);
			file.mkdirs();

			InputStream is = uploadItem.getInputStream();
			String filename = String.valueOf(System.currentTimeMillis()) + ".xls";
			File uploadedFile = new File(uploadDir + "/" + filename);
			DataOutputStream out = null;
			out = new DataOutputStream(new BufferedOutputStream(
					new FileOutputStream(uploadedFile)));
			while (true) {
				int value = is.read();
				out.write(value);
				if (value < 0) {
					break;
				}
			}
			out.close();
			uploadFile(uploadedFile);

		} catch (IOException e) {
			logger.error(e);
		}
	}

	public void initialise() {
		List<Course> result = personService.getAllTraining();
		courses = new HashMap<String, Course>();
		for (Course course : result) {
			courses.put(course.getName(), course);
		}

		List<Rto> rtoResult = personService.getAllRTOs();
		rtos = new HashMap<String, Rto>();
		for (Rto rto : rtoResult) {
			rtos.put(rto.getName(), rto);
		}

		List<Person> personResultList = personService.getAllPersonDetails();
		persons = new HashMap<Integer, Person>();
		for (Person person : personResultList) {
			persons.put(person.getId(), person);
		}

		List<Workercompany> wcResultList = personService.getAllCompanies();
		companies = new HashMap<String, Workercompany>();
		for (Workercompany company : wcResultList) {
			companies.put(company.getName() + company.getSite(), company);
		}

		List<Fundingsource> allFundingSources = personService
				.getAllFundingSources();
		fundingSources = new HashMap<String, Fundingsource>();
		for (Fundingsource fs : allFundingSources) {
			fundingSources.put(fs.getSourceName(), fs);
		}
	}

	public static void main(String... args) {
		ExcelTCVVicFormat f = new ExcelTCVVicFormat();
		f
				.uploadFile(new File(
						"F:/Dev/daves79/trunk/TCFVic/Pac_Brands_All_Data_-_Kayser_-_Gangur_to_Huynh.xls"));
	}

	public static void main2(String... args) {
		File file = new File("G:/jayser spreadsheets");
		for (String s : file.list()) {
			ExcelTCVVicFormat f = new ExcelTCVVicFormat();
			f.uploadFile(new File("G:/kayser spreadsheets/" + s));
		}
	}

	public void uploadFile(File file) {
		logger.info("In ExcelTCVVicFormat.uploadFile");

		try {
			Workbook workbook = createWorkbook(file);
			if (workbook.getSheets() != null) {
				processSheets(workbook);
			}
		} catch (Exception e) {
			logger.error(e);
		}
	}

	public void processSheets(Workbook workbook) throws Exception {
		for (Sheet sheet : workbook.getSheets()) {
			Person person = null;
			try {
				String personNameAlreadyInDatabase = getNameIfPersonExists(sheet);
				if (personNameAlreadyInDatabase != null) {
					String msg = "Person not imported as they already exist - "
							+ personNameAlreadyInDatabase;
					errorList.add(msg);
					logger.warn(msg);
					continue;
				}

				try {
					person = processPersonRecord(sheet);
				} catch (Exception e) {
					String msg = "Sheet not processed due to: "
							+ e.getMessage();
					errorList.add(msg);
					logger.warn(msg);
					continue;
				}
				processFundingSources(person);
				person.setEmploymentdetailses(processCompany(sheet, person));
				person.setPersoncourses(processPersonCourses(sheet, person));

				personService.savePersonDetails(person);
				numberImported++;
			} catch (Exception e) {
				logger
						.error("An error occured importing data for "
								+ person.getFirstname() + ", "
								+ person.getSurname(), e);
			}
		}
	}

	private Set<Employmentdetails> processCompany(Sheet sheet, Person person)
			throws Exception {
		// Company details
		String cellEmployeeNo = "C14";
		// String cellCompanySite = "C15";
		String cellDivision = "C16";
		Cell employeeNo = sheet.getCell(cellEmployeeNo);
		String companySite = "Kayser";
		Cell division = sheet.getCell(cellDivision);

		if (!companies.containsKey(PAC_BRANDS_NAME + companySite)) {
			Workercompany company = new Workercompany();
			company.setName(PAC_BRANDS_NAME);
			company.setSite(companySite);
			personService.saveCompanyDetails(company);
			initialise();
		}
		String companySiteName = companySite;
		if (companySiteName == null) {
			companySiteName = "";
		}
		Set<Employmentdetails> employmentdetailsSet = new HashSet<Employmentdetails>();
		Workercompany company = companies
				.get(PAC_BRANDS_NAME + companySiteName);
		Employmentdetails empDetails = new Employmentdetails();
		empDetails.setId(new EmploymentdetailsId(company.getId(), person
				.getId()));
		if (StringUtils.isEmpty(employeeNo.getContents())) {
			logger.warn("While importing from file, " + person.getFirstname()
					+ " " + person.getSurname()
					+ " has no employee number registerd");
			errorList.add(person.getFirstname() + " " + person.getSurname()
					+ " has no employee number registerd");
		}
		empDetails.setEmployeenumber(employeeNo.getContents());
		empDetails.setDivision(division.getContents());
		employmentdetailsSet.add(empDetails);
		person.setEmploymentdetailses(employmentdetailsSet);

		return employmentdetailsSet;
	}

	private Set<Personcourse> processPersonCourses(Sheet sheet, Person person)
			throws Exception {
		Set<Personcourse> pcSet = new HashSet<Personcourse>();
		int i = 2;
		int j = 0;
		while (true) {
			i++;
			j++;
			Cell courseNumber = sheet.getCell(i, 1);
			if (courseNumber.getContents().equalsIgnoreCase("Course " + j)) {
				Cell courseName = sheet.getCell(i, 21);
				Cell rtoName = sheet.getCell(i, 22);
				Cell trainingDates = sheet.getCell(i, 23);
				Cell datePBGSignedOff = sheet.getCell(i, 24);
				Cell cost = sheet.getCell(i, 25);
				// Cell otherExpense = sheet.getCell(i, 26);
				Cell skillUpPayment = sheet.getCell(i, 28);
				Cell pacBrandsPayment = sheet.getCell(i, 29);
				Cell tcfSAPPayment = sheet.getCell(i, 30);
				Cell SAP_PPP = sheet.getCell(i, 31);

				if (courseName == null || courseName.getContents() == null
						|| courseName.getContents().trim().length() == 0) {
					continue;
				}
				
				Course course = processCourse(courseName, rtoName, cost);
				
				// process expense for course
				// processExpense(person, otherExpense);
				processExpenseWithFS(person, fundingSources.get(SKILL_UP_NAME),
						skillUpPayment, course);
				processExpenseWithFS(person, fundingSources
						.get(PAC_BRANDS_NAME), pacBrandsPayment, course);
				processExpenseWithFS(person, fundingSources.get(TCFSAP_NAME),
						tcfSAPPayment, course);

				// special handling of this expense, assign $1 if there is a
				// value
				if (SAP_PPP != null && SAP_PPP.getContents() != null
						&& !SAP_PPP.getContents().isEmpty()) {
					processExpenseWithFS(person, fundingSources
							.get(SAP_PPP_NAME), "1", course,
							"No courses");
				}

				// Fundingsource fs = fundingSources.get();

				Personcourse pc = new Personcourse();
				pc.setRto(processRTO(rtoName.getContents()));
				// pc.setCourse(course);
				try {
					pc.setTrainingdate(((DateCell) trainingDates).getDate());
					pc.setFinishDate(((DateCell) trainingDates).getDate());
				} catch (Exception e) {
					logger
							.info("  OK to ignore: " + person.getFirstname()
									+ " "
									+ person.getSurname()
									+ ": Training date could not be extracted - trying alternative format 'dd/MM/yyyy - dd/MM/yyyy': "
									+ trainingDates.getContents());
					try {
						String delimeter = "-";
						if (trainingDates.getContents().contains("&")) {
							delimeter = "&";
						}
						String startDate = trainingDates.getContents().split(
								delimeter)[0].trim();
						if (startDate.length() > 10) {
							throw new Exception("Start date is invalid length");
						}
						pc.setTrainingdate(sdf.parse(startDate));
						String endDate = trainingDates.getContents().split(
								delimeter)[1].trim();
						if (endDate.length() > 10) {
							throw new Exception("Finish date is invalid length");
						}
						pc.setFinishDate(sdf.parse(endDate));
					} catch (Exception e1) {
						logger.info("  OK to ignore: " + person.getFirstname() + " "
								+ person.getSurname()
								+ ": Training date could not be extracted - "
								+ trainingDates.getContents());
					}
				}
				try {
					pc
							.setDatesignedoff(((DateCell) datePBGSignedOff)
									.getDate());
				} catch (Exception e) {
					logger.warn(person.getFirstname() + " "
							+ person.getSurname()
							+ ": Date signed off could not be extracted - "
							+ datePBGSignedOff.getContents());
				}

				// Create PCFS list
				Set<Personcoursefundingsource> pcfsSet = new HashSet<Personcoursefundingsource>();

				// 1st PCFS
				processPCFS(pacBrandsPayment, pc, pcfsSet,
						PAC_BRANDS_NAME, PAC_BRANDS_DELIVERED_VIA, course);
				processPCFS(skillUpPayment, pc, pcfsSet,
						SKILL_UP_NAME, SKILL_UP_DELIVERED_VIA, course);
				processPCFS(tcfSAPPayment, pc, pcfsSet,
						TCFSAP_NAME, TCFSAP_DELIVERED_VIA, course);
				processPCFS(SAP_PPP, pc, pcfsSet,
						SAP_PPP_NAME, SAP_PPP_DELIVERED_VIA, course);

				pc.setPersoncoursefundingsources(pcfsSet);
				pc.setId(new PersoncourseId(0, person.getId(), course.getId()));
				pcSet.add(pc);

			} else {
				break;
			}
		}
		return pcSet;
	}

	private Expense processExpense(Person person, Cell otherExpense)
			throws Exception {
		Expense expense = new Expense();
		try {
			if (otherExpense.getContents() != null
					&& !otherExpense.getContents().isEmpty()) {
				expense.setAmount(Double.valueOf(otherExpense.getContents()));
				expense.setId(new ExpenseId(0, person.getId()));
				personService.saveExpense(expense);
			}
		} catch (Exception e) {
			logger.warn("Could not process expense for:  "
					+ person.getSurname() + ", " + person.getFirstname(), e);
		}
		return expense;
	}

	private Expense processExpenseWithFS(Person person, Fundingsource fs,
			Cell otherExpense, Course course)
			throws Exception {
		Expense expense = new Expense();
		if (otherExpense != null && otherExpense.getContents() != null
				&& !otherExpense.getContents().isEmpty()) {
			expense = processExpenseWithFS(person, fs, otherExpense
					.getContents(), course, "Dollars");
		}
		return expense;
	}

	private Expense processExpenseWithFS(Person person, Fundingsource fs,
			String expenseAmount, Course course, String unit)
			throws Exception {
		Expense expense = new Expense();
		try {
			if (expenseAmount != null) {
				expense.setFundingsource(fs);
				expense.setRelatedCourse(String.valueOf(course.getId()));
				try {
					expense.setAmount(Double.valueOf(expenseAmount));
					expense.setUnits(unit);
				} catch (Exception e) {
				}
				expense.setId(new ExpenseId(0, person.getId()));
				personService.saveExpense(expense);
			}
		} catch (Exception e) {
			logger.warn("Could not process expense (course name: " + course.getName()
					+ ") for:  " + person.getSurname() + ", "
					+ person.getFirstname(), e);
		}
		return expense;
	}

	private void processPCFS(Cell cost, 
			Personcourse pc, Set<Personcoursefundingsource> pcfsSet,
			String sourceName, String businessName, Course course) {
		if (cost.getContents() != null
				&& cost.getContents().trim().length() > 0) {
			Personcoursefundingsource pcfs = new Personcoursefundingsource();
			Fundingsource fs = getFundingSource(sourceName, businessName);
			pcfs.setFundingsource(fs);
			pcfs.setPersoncourse(pc);
			try {
				pcfs.setCost(Double.valueOf(cost.getContents()));
			} catch (NumberFormatException e) {
			}
			pcfs.setId(new PersoncoursefundingsourceId(fs.getId(), 0, 0, course
					.getId()));
			pcfsSet.add(pcfs);
		}
	}

	private Fundingsource getFundingSource(String sourceName,
			String deliveredVia) {
		for (Fundingsource fsCheck : fundingSources.values()) {
			if (fsCheck.getSourceName().equals(sourceName)
					&& fsCheck.getDeliveredvia() != null
					&& fsCheck.getDeliveredvia().equals(deliveredVia)) {
				return fsCheck;
			}
		}
		return null;
	}

	private void processFundingSources(Person person) throws Exception {
		Set<Personfundingsource> pfsSet = new HashSet<Personfundingsource>();

		Fundingsource fs = insertFundingSource(PAC_BRANDS_NAME,
				PAC_BRANDS_DELIVERED_VIA, PAC_BRANDS_INITIAL_AMOUNT,
				PAC_BRANDS_INITIAL_DAYS, PAC_BRANDS_INITIAL_HOURS, "", "", "");
		pfsSet.add(saveConnectPersonFundingSource(fs, person));

		fs = insertFundingSource(SKILL_UP_NAME, SKILL_UP_DELIVERED_VIA,
				SKILL_UP_INITIAL, 0, 0, "Lauren Best", "",
				"lauren.best@diird.vic.gov.au");
		pfsSet.add(saveConnectPersonFundingSource(fs, person));

		fs = insertFundingSource(TCFSAP_NAME, TCFSAP_DELIVERED_VIA,
				TCFSAP_INITIAL, 0, 0, "Jodie Wearne", "02 6121 3580",
				"jodie.wearne@deewr.gov.au");
		pfsSet.add(saveConnectPersonFundingSource(fs, person));

		fs = insertFundingSource(SAP_PPP_NAME, SAP_PPP_DELIVERED_VIA,
				SAP_PPP_INITIAL, 0, 0, "Peter Cook", "",
				"peter.cook@deewr.gov.au");
		pfsSet.add(saveConnectPersonFundingSource(fs, person));

		person.setPersonfundingsources(pfsSet);
	}

	private Fundingsource insertFundingSource(String sourceName,
			String businessName, double initialAmount, long daysAllowed,
			long hoursAllowed, String contactpeople, String tel1, String email)
			throws Exception {
		Fundingsource fs = new Fundingsource();
		fs.setSourceName(sourceName);
		fs.setDeliveredvia(businessName);
		fs.setInitialamount(initialAmount);
		fs.setHours(hoursAllowed);
		fs.setDays(daysAllowed);
		fs.setContactpeople(contactpeople);
		fs.setTel1(tel1);
		fs.setEmail(email);

		// Set<Personfundingsource> pfsSet = new HashSet<Personfundingsource>();
		// check if FS doesn't already exist
		boolean fsExists = false;
		for (Fundingsource fsCheck : fundingSources.values()) {
			if (/* fsCheck.getBusinessName().equals(fs.getBusinessName()) */
			fsCheck.getSourceName().equals(fs.getSourceName())) {
				fsExists = true;
			}
		}
		if (!fsExists) {
			personService.saveFundingSourceDetails(fs);
		}
		initialise();
		return getFundingSource(fs.getSourceName(), fs.getDeliveredvia());
	}

	private Personfundingsource saveConnectPersonFundingSource(
			Fundingsource fs, Person person) {
		Personfundingsource pfs = new Personfundingsource();
		pfs.setId(new PersonfundingsourceId(fs.getId(), person.getId()));
		pfs.setInitialamount(fs.getInitialamount());
		return pfs;
	}

	private Course processCourse(Cell courseName, Cell rtoName, Cell cost)
			throws Exception {
		// get course or create course
		Course course = courses.get(courseName.getContents().trim());
		if (course == null) {
			course = new Course();
			course.setName(courseName.getContents().trim());
			try {
				course.setCost(Double.valueOf(cost.getContents()));
			} catch (Exception e) {
			}
			personService.saveTrainingDetails(course);
			initialise();
			course = courses.get(courseName.getContents().trim());
		}
		return course;
	}

	private Rto processRTO(String rtoName) throws Exception {
		if (rtos.containsKey(rtoName)) {
			return rtos.get(rtoName);
		} else {
			Rto rto = new Rto();
			rto.setName(rtoName);
			personService.saveRTO(rto);
			initialise();
			return rtos.get(rtoName);
		}
	}

	private String getNameIfPersonExists(Sheet sheet) {
		String cellSurname = "B3";
		String cellFirstname = "B4";

		Cell surname = sheet.getCell(cellSurname);
		Cell firstname = sheet.getCell(cellFirstname);

		for (Person person : persons.values()) {
			if (person.getSurname().equals(surname.getContents())
					&& person.getFirstname().equals(firstname.getContents())) {
				return "Firstname: " + person.getFirstname() + ", Surname: "
						+ person.getSurname();
			}
		}
		return null;
	}

	private Person processPersonRecord(Sheet sheet) throws Exception {
		String cellSurname = "B3";
		String cellFirstname = "B4";
		String cellStreetaddress = "B5";
		String cellSuburb = "B6";
		String cellState = "B7";
		String cellPostcode = "B8";
		String cellPhone = "B9";
		String cellMobile = "B10";
		String cellEmail = "B11";
		String cellUnionMemberNo = "B12";

		Cell surname = sheet.getCell(cellSurname);
		Cell givenName = sheet.getCell(cellFirstname);

		if (givenName.getContents() == null
				|| givenName.getContents().isEmpty()) {
			throw new Exception("Sheet is missing given name");
		}

		if (surname.getContents() == null || surname.getContents().isEmpty()) {
			throw new Exception("Sheet is missing surname");
		}
		logger.info("Importing: " + givenName + " " + surname);
		Cell streetaddress = sheet.getCell(cellStreetaddress);
		Cell suburb = sheet.getCell(cellSuburb);
		Cell state = sheet.getCell(cellState);
		Cell postcode = sheet.getCell(cellPostcode);
		Cell phone = sheet.getCell(cellPhone);
		Cell mobile = sheet.getCell(cellMobile);
		Cell email = sheet.getCell(cellEmail);
		Cell unionMemberNo = sheet.getCell(cellUnionMemberNo);

		Person person = new Person();
		Address address = new Address();
		if (surname != null) {
			person.setSurname(surname.getContents());
		}
		if (givenName != null) {
			person.setFirstname(givenName.getContents());
		}
		if (streetaddress != null) {
			address.setStreet(streetaddress.getContents());
		}
		if (suburb != null) {
			address.setSuburb(suburb.getContents());
		}
		if (state != null) {
			String stateStr = state.getContents();
			if ("VICTORIA".equals(stateStr)) {
				stateStr = "VIC";
			}
			address.setState(stateStr);
		}
		if (postcode != null) {
			try {
				address.setPostcode(new Integer(postcode.getContents()));
			} catch (Exception e) {
			}
		}
		if (phone != null) {
			person.setLandline(phone.getContents());
		}
		if (mobile != null) {
			person.setMobile(mobile.getContents());
		}
		if (email != null) {
			person.setEmail(email.getContents());
		}
		if (unionMemberNo != null) {
			person.setUnionMemberNo(unionMemberNo.getContents());
		}
		// if (unionMemberNo != null) {
		// p.setu(unionMemberNo.getContents());
		// }
		Personaddress personAddress = new Personaddress();
		personAddress.setAddress(address);
		Set<Personaddress> personAddresses = new HashSet<Personaddress>();
		personAddresses.add(personAddress);
		person.setPersonaddresses(personAddresses);

		person = personService.savePersonDetails(person);
		return person;
	}

	public Workbook createWorkbook(File file) throws BiffException, IOException {
		return Workbook.getWorkbook(file);
	}

}
